class Sell():
    def __init__(self, id, nro_factura, date, client, total, items, pays, seller):
        self.id = id
        self.nro_factura = nro_factura
        self.date = date
        self.client = client
        self.total = total
        self.items = items
        self.pays = pays
        self.seller = seller
        
    def __eq__(self, other):
        return self.id == other.id

class SellPay():  
    def __init__(self, idformapagos, fecha, monto, cuotas):
        self.idformapagos = idformapagos
        self.fecha = fecha
        self.monto = monto
        self.cuotas = cuotas
        
class Club():
    def __init__(self, id, client, category, state, request_date, deliver_date, sucursal):
        self.id = id
        self.client = client
        self.category = category
        self.state = state
        self.request_date = request_date
        self.deliver_date = deliver_date
        self.sucursal = sucursal
        
    def __eq__(self, other):
        return self.id == other.id        
        
class SellItem():  
    def __init__(self, product, amount, total):
        self.product = product
        self.amount = amount
        self.total = total
        
class Valor():
    def __init__(self, nombre, descripcion, valor, tipo, tipoVenta):
        self.nombre = nombre
        self.descripcion = descripcion
        self.valor = valor
        self.tipo = tipo
        self.tipoVenta = tipoVenta

class Product():
    def __init__(self, id, desc, codsussan, color, talle, provider, group, price, year):
        self.id = id
        self.desc = desc
        self.codsussan = codsussan
        self.color = color
        self.talle = talle
        self.provider = provider
        self.group = group
        self.price = price
        self.year = year
        
    def __eq__(self, other):
        return self.id == other.id

class ProductGroup():
    def __init__(self, id, name):
        self.id = id
        self.name = name
        
    def __eq__(self, other):
        return self.id == other.id        

class Provider():
    def __init__(self, id, name):
        self.id = id
        self.name = name
        
    def __eq__(self, other):
        return self.id == other.id
        
class Vendor():
    def __init__(self, id, name):
        self.id = id
        self.name = name
        
    def __eq__(self, other):
        return self.id == other.id

class Rule():
    def __init__(self, id, name, points, year, month, specs, sub_rules):
        self.id = id
        self.sub_rules = sub_rules
        self.specs = specs
        self.name = name
        self.points = points
        self.year = year
        self.month = month
        
    def apply(self, sell):
        for s in self.specs:
            if not s.apply(sell):
                return False
                
        for r in self.sub_rules:
            if not r.apply(sell):
                return False
                
        return True
    def __eq__(self, other):
        return self.id == other.id

class RuleSpec():
    def __init__(self, id, f, t, elem, amount, year, month, type):
        self.id = id
        self.f = f
        self.t = t
        self.elements = elem
        self.amount = amount
        self.year = year
        self.month = month
        self.type = type
    
    def apply(sell):
        pass

class ProviderSpec(RuleSpec):  
    def apply(self, sell):
        amount = 0
        for i in sell.items:
            product = i.product
            #print product.provider.name
            if product!=None:
				if product.provider.name in self.elements:
				    amount = amount + int(i.amount)
        
        if amount >= int(self.amount):
            return True
        
        return False

class GroupSpec(RuleSpec):
    def apply(self, sell):
        amount = 0
        for i in sell.items:
            product = i.product
            if product!=None:
                if product.group.name in self.elements:
                    amount = amount + int(i.amount)
                
        if amount >= int(self.amount):
            return True                
                
        return False
        
class PayTypeSpec(RuleSpec):
    def apply(self, sell):
        for i in sell.pays:
            if i.idformapagos in self.elements:
                return True
                
        return False        

class AmountItemSpec(RuleSpec):
    def apply(self, sell):
        total_items = 0
        for item in sell.items:
            total_items += item.amount
        
        #if self.f != -99999 and total_items < self.f: return False
        #if self.t != 99999 and total_items >= self.t: return False
        
        #return True
        
        return total_items >= self.f and total_items < self.t
        
class AmountMoneySpec(RuleSpec):
    def apply(self, sell):
        return sell.total >= self.f and sell.total < self.t

class CodsussanSpec(RuleSpec):
    def apply(self, sell):
        total_items = 0
        for item in sell.items:
            if item.product.codsussan in self.elements:
                total_items += item.amount
                
        return total_items >= self.f
        
class YearMonthSpec(RuleSpec):
    def apply(self, sell):
        for i in sell.items:
            if int(i.product.year) == int(self.year):
                return True
                
        return False  
        